#include "Socket.hpp"
#include "memory"
#include "Usage.hpp"
#include "Protocol.hpp"
#include <cstring>


// ./tcpclient 127.0.0.1 8888
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0], "1");
        exit(-1);
    }
    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);
    std::unique_ptr<Socket> socket = std::make_unique<TcpSocket>();
    if (socket->BuildConnectionSocketMethod(serverip, serverport))
    {
        std::cout << "connect success, _sockfd: " << socket->GetSockfd() << std::endl;
    }
    std::unique_ptr<Factory> factory = std::make_unique<Factory>();
    std::shared_ptr<Protocol> req = factory->BuildRequest(10, 20 , '+');
    while (true)
    {
        // int a, b;
        // std::cout << "Please Print# ";
        // std::cin >> a >> b;
        // char c;
        // std::cin >> c;
        req->Print();
        ssize_t n = send(socket->GetSockfd(), &(*req), sizeof(*req), 0);
        // std::cout << "no error" << std::endl;
        // std::shared_ptr<Result> res;
        Result res;
        recv(socket->GetSockfd(), &(res), sizeof(res), 0);
        res.Print();
        std::string inbuffer;
        inbuffer.clear();
        socket->Recv(&inbuffer, 1024);
        std::cout << inbuffer << std::endl <<std::endl;
        req->Inc();
        sleep(5);
    }
    socket->CloseSocket();
    return 0;
}